package com.ruoyi.wx.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.wx.domain.Merchant;
import com.ruoyi.wx.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.wx.domain.MerchantVerification;
import com.ruoyi.wx.service.IMerchantVerificationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商户认证Controller
 * 
 * @author wx
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/verification")
public class MerchantVerificationController extends BaseController
{
    @Autowired
    private IMerchantVerificationService merchantVerificationService;

    @Autowired
    private IMerchantService merchantService;

    /**
     * 查询商户认证列表
     */
    @RequiresPermissions("wx:verification:list")
    @GetMapping("/list")
    public TableDataInfo list(MerchantVerification merchantVerification)
    {
        startPage();
        List<MerchantVerification> list = merchantVerificationService.selectMerchantVerificationList(merchantVerification);
        return getDataTable(list);
    }

    /**
     * 查询商户认证wwc
     */
//    @RequiresPermissions("wx:verification:list")
    @GetMapping("/getMerchant")
    public R<?> getMerchant(MerchantVerification merchantVerification)
    {
        MerchantVerification verification = merchantVerificationService.selectMerchantVerificationByMobile(merchantVerification);
        if (verification != null){

            Merchant merchant = merchantService.selectMerchantByMId(verification.getMid());
            if (merchant!= null && merchant.getAuditStatusId() == 2){
                return R.ok(verification);
            }else {
                return R.fail("未审核或审核未通过！");
            }

        }else {
            return R.fail("没有对应的商户认证表！");
        }

    }

    /**
     * 导出商户认证列表
     */
    @RequiresPermissions("wx:verification:export")
    @Log(title = "商户认证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MerchantVerification merchantVerification)
    {
        List<MerchantVerification> list = merchantVerificationService.selectMerchantVerificationList(merchantVerification);
        ExcelUtil<MerchantVerification> util = new ExcelUtil<MerchantVerification>(MerchantVerification.class);
        util.exportExcel(response, list, "商户认证数据");
    }

    /**
     * 获取商户认证详细信息
     */
    @RequiresPermissions("wx:verification:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(merchantVerificationService.selectMerchantVerificationById(id));
    }

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 新增商户认证
     */
//    @RequiresPermissions("wx:verification:add")
    @Log(title = "商户认证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantVerification merchantVerification)
    {
        SysUser sysUser = new SysUser();
        sysUser.setUserName(merchantVerification.getDirector());
        sysUser.setPhonenumber(merchantVerification.getMobile());
        sysUser.setNickName(merchantVerification.getDirector());
        sysUser.setPassword("123456");
        Long[] roleIds = {100L};
        sysUser.setRoleIds(roleIds); // 商户角色组
        SysUser sysUser1 = remoteUserService.addSysUser(sysUser, SecurityConstants.INNER);
        System.out.println("sysUser1 = " + sysUser1);
        merchantVerification.setSysId(sysUser1.getUserId());

        return toAjax(merchantVerificationService.insertMerchantVerification(merchantVerification));
    }


    /**
     * 修改商户认证
     */
//    @RequiresPermissions("wx:verification:edit")
    @Log(title = "商户认证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantVerification merchantVerification)
    {
        return toAjax(merchantVerificationService.updateMerchantVerification(merchantVerification));
    }

    /**
     * 删除商户认证
     */
    @RequiresPermissions("wx:verification:remove")
    @Log(title = "商户认证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(merchantVerificationService.deleteMerchantVerificationByIds(ids));
    }
}
